package com.ruoyi.business.service.koc.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.domain.dto.admin.koc.TbUserKocPointsLogPageQueryDTO;
import com.ruoyi.business.domain.koc.*;
import com.ruoyi.business.domain.vo.*;
import com.ruoyi.business.mapper.koc.TbUserKocPointsLogMapper;
import com.ruoyi.business.service.koc.*;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 用户获取积分流水Service业务层处理
 *
 * @author ruoyi
 * @date 2025-09-11
 */
@Service
public class TbUserKocPointsLogServiceImpl extends ServiceImpl<TbUserKocPointsLogMapper, TbUserKocPointsLog> implements ITbUserKocPointsLogService {

    @Resource
    private ITbUserKocTaskInfoService tbUserKocTaskInfoService;

    @Resource
    private ITbUserKocTaskInfoDetailService tbUserKocTaskInfoDetailService;

    @Resource
    private ITbKocTaskService tbKocTaskService;

    @Resource
    private ITbUserService tbUserService;

    @Override
    public TableDataInfo<TbUserKocPointsLogPageVO> selectPageList(TbUserKocPointsLogPageQueryDTO tbUserKocPointsLogPageQueryDTO, PageQuery pageQuery) {
        Page<TbUserKocPointsLogPageVO> page = pageQuery.build();
        IPage<TbUserKocPointsLogPageVO> pageRes = baseMapper.selectPageList(page, tbUserKocPointsLogPageQueryDTO);
        return TableDataInfo.build(pageRes);
    }

    @Override
    public TraceabilityDetailVO selectDetail(long sourceId) {
        TbUserKocTaskInfo tbUserKocTaskInfo = tbUserKocTaskInfoService.getOne(Wrappers.lambdaQuery(TbUserKocTaskInfo.class).eq(TbUserKocTaskInfo::getCheckBusinessId, sourceId).last(Constants.MYBATIS_PLUSH_LIMIT_ONE));
        TbUserKocTaskInfoUserDetailVO tbUserKocTaskInfoUserDetailVO = BeanUtil.toBean(tbUserKocTaskInfo, TbUserKocTaskInfoUserDetailVO.class);
        TbUser tbUser = tbUserService.getById(tbUserKocTaskInfo.getUserId());
        TbKocTask tbKocTask = tbKocTaskService.getById(tbUserKocTaskInfo.getKocTaskId());
        List<TbUserKocTaskInfoDetail> detailList = tbUserKocTaskInfoDetailService.list(Wrappers.lambdaQuery(TbUserKocTaskInfoDetail.class).eq(TbUserKocTaskInfoDetail::getKocTaskInfoId, tbUserKocTaskInfo.getId()));
        List<TbUserKocTaskInfoDetailVO> tbUserKocTaskInfoDetailVOS = BeanUtil.copyToList(detailList, TbUserKocTaskInfoDetailVO.class);
        tbUserKocTaskInfoUserDetailVO.setTbUserKocTaskInfoDetails(tbUserKocTaskInfoDetailVOS);
        return TraceabilityDetailVO.builder().tbKocTask(tbKocTask).tbUserKocTaskInfos(tbUserKocTaskInfoUserDetailVO).tbUser(tbUser).build();
    }

    @Override
    public TableDataInfo<TbUserKocPointsLog> queryMyPointLogsPage(Long userId, Long checkBusinessId, String kocDate, Long taskId, Integer type, Integer sourceType, PageQuery pageQuery) {
        LambdaQueryWrapper<TbUserKocPointsLog> tbUserKocPointsLogLambdaQueryWrapper = Wrappers.lambdaQuery(TbUserKocPointsLog.class);
        tbUserKocPointsLogLambdaQueryWrapper.eq(TbUserKocPointsLog::getUserId, userId);
        tbUserKocPointsLogLambdaQueryWrapper.eq(ObjectUtils.isNotEmpty(checkBusinessId), TbUserKocPointsLog::getSourceId, checkBusinessId);
        tbUserKocPointsLogLambdaQueryWrapper.eq(ObjectUtils.isNotEmpty(type), TbUserKocPointsLog::getType, type);
        tbUserKocPointsLogLambdaQueryWrapper.eq(ObjectUtils.isNotEmpty(sourceType), TbUserKocPointsLog::getSourceType, sourceType);
        tbUserKocPointsLogLambdaQueryWrapper.eq(ObjectUtils.isNotEmpty(taskId), TbUserKocPointsLog::getKocTaskId, taskId);
        tbUserKocPointsLogLambdaQueryWrapper.eq(StringUtils.isNotEmpty(kocDate), TbUserKocPointsLog::getKocDate, kocDate);
        tbUserKocPointsLogLambdaQueryWrapper.orderByDesc(TbUserKocPointsLog::getCreateTime);
        Page<TbUserKocPointsLog> page = baseMapper.selectPage(pageQuery.build(), tbUserKocPointsLogLambdaQueryWrapper);
        return TableDataInfo.build(page);
    }

    @Override
    public TbUserKocPointsRankVO userPointsRank() {
        String format = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM"));
        return baseMapper.userPointsRank(format);
    }

    @Override
    public List<TbUserKocTaskInfo> queryBySourceId(Long sourceId) {
        return tbUserKocTaskInfoService.getBaseMapper().selectList(Wrappers.lambdaQuery(TbUserKocTaskInfo.class)
            .eq(TbUserKocTaskInfo::getCheckBusinessId, sourceId));
    }

    @Override
    public TableDataInfo<TbUserKocPointsLogStatisticVO> queryMyPointLogsStatisticPage(Long userId, String kocDate, PageQuery pageQuery) {
        Page<TbUserKocPointsLogStatisticVO> page = pageQuery.build();
        IPage<TbUserKocPointsLogStatisticVO> pageRes = baseMapper.queryMyPointLogsStatisticPage(page, userId, kocDate);
        return TableDataInfo.build(pageRes);


    }

}
